package com.jxmcloud.business.cut.service.impl.app.activity;

import cn.hutool.core.util.ObjectUtil;
import com.jxmcloud.business.cut.constants.CutErrorMsg;
import com.jxmcloud.business.cut.dto.client.GetNextFlowInfoDto;
import com.jxmcloud.business.cut.dtoCommon.CashDetailInfoDto;
import com.jxmcloud.business.cut.json.DataConfigService;
import com.jxmcloud.business.cut.service.app.activity.IFlowInfoService;
import com.jxmcloud.business.cut.service.database.IUserProgressLogDBService;
import com.jxmcloud.business.cut.service.user.IUserActivityService;
import com.jxmcloud.business.cut.service.user.IUserCashDetailListService;
import com.jxmcloud.business.cut.service.user.IUserFlowService;
import com.jxmcloud.business.cut.util.MyTool;
import com.jxmcloud.business.cut.vo.client.GetNextFlowInfoVo;
import com.jxmcloud.business.cut.vo.json.ConfigEventVo;
import com.jxmcloud.business.cut.vo.user.PlayerInfoVo;
import com.jxmcloud.business.cut.vo.user.UserCashDetailItemVo;
import com.jxmcloud.business.cut.vo.user.UserFlowVo;
import com.jxmcloud.business.cut.vo.user.UserOrderListVo;
import com.xlcloud.platform.api.config.Constant;
import com.xlcloud.platform.api.exception.BusinessException;
import com.xlcloud.platform.util.PublicUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Objects;
import java.util.stream.Collectors;

@Service
@Slf4j
public class FlowInfoServiceImpl implements IFlowInfoService {
    @Resource
    private DataConfigService dataConfigService;
    @Resource
    private IUserFlowService userFlowService;
    @Resource
    private IUserCashDetailListService userCashDetailListService;
    @Resource
    private IUserProgressLogDBService userProgressLogDBService;

    // 获取下一步流程
    @Override
    public GetNextFlowInfoVo getNextFlowInfo(GetNextFlowInfoDto dto, PlayerInfoVo player) {
        // 数据填充
        GetNextFlowInfoVo ansData = new GetNextFlowInfoVo();
        String error = CutErrorMsg.ERROR_TRUE;
        do {
            UserFlowVo flowInfo = player.getFlowInfo();
            // 如果上一步一样
            if (flowInfo.getPreId().equals(dto.getCurStepId()) && !flowInfo.getPreId().equals(flowInfo.getStepId())) {
                ansData.setNextStepId(flowInfo.getStepId());
                // 获取零钱的记录信息
                UserCashDetailItemVo cashDetail = userCashDetailListService.getCashDetailItemByProgressId(player, flowInfo.getStepId());
                if (ObjectUtil.isNotNull(cashDetail)) {
                    CashDetailInfoDto packData = userCashDetailListService.packVotoDto(cashDetail);
                    ansData.setCashDetail(packData);
                }
                break;
            }
            // 如果当前步骤一样
            if (!flowInfo.getStepId().equals(dto.getCurStepId())) {
                error = CutErrorMsg.ERROR_NO_STEP;
                break;
            }
            // 检测进入直播
            String testErr = userFlowService.testEnterLiveStepErr(player);
            if (!testErr.isEmpty()) {
                error = testErr;
                break;
            }
            // 是否执行了事件
            boolean isRunEvent = false;
            for (Integer eventId : dto.getEventIds()) {
                // 判断事件是否有效
                boolean checkFlag = userFlowService.checkEventValid(player, dto.getCurStepId(), eventId);
                if (!checkFlag) {
                    // 无效事件
                    continue;
                }
                // 条件判断
                checkFlag = userFlowService.checkFlowEvent(player, eventId);
                if (!checkFlag) {
                    // 无效事件
                    continue;
                }
                // 执行事件
                boolean flag = userFlowService.runEvent(player, eventId);
                if (!flag) {
                    continue;
                }
                isRunEvent = true;
                // 获取零钱的记录信息
                UserCashDetailItemVo cashDetail = userCashDetailListService.getCashDetailItemByProgressId(player, flowInfo.getStepId());
                if (ObjectUtil.isNotNull(cashDetail)) {
                    CashDetailInfoDto packData = userCashDetailListService.packVotoDto(cashDetail);
                    ansData.setCashDetail(packData);
                }
                break;
            }

            // 判断接回原有步骤
            // 有复活步骤
            Integer reviveStepId = player.getFlowInfo().getReviveStepId();
            if (!isRunEvent && MyTool.isTure(reviveStepId)) {
                userFlowService.backToReviveStep(player);
                log.info("复活结束步骤, 接回原有步骤");
            }
        } while (false);
        if (!error.equals(CutErrorMsg.ERROR_TRUE)) {
            // 抛出异常
            throw new BusinessException(error);
        }
        ansData.setNextStepId(player.getFlowInfo().getStepId());

        // 记录当前步骤和下一步
        userProgressLogDBService.insertUserProgressLog(player);
        return ansData;
    }

    /**
     * 查找下一步流程
     * @param userOrderTranVos
     * @param envId
     * @return
     */
    private Integer getNextId(UserOrderListVo userOrderTranVos, Long envId) {
        Integer nextId = 0;
        ConfigEventVo configEventConfig = dataConfigService.getConfigEventConfig(Math.toIntExact(envId));
        PublicUtil.ifNullAndThrow(configEventConfig, envId + ":事件id不存在！");
        if (Objects.isNull(configEventConfig.getCondition())) {
            nextId = configEventConfig.getProgressId();
            return nextId;
        }
        //条件 有 购物记录
        if (Constant.INT_ONE.equals(configEventConfig.getCondition()) && PublicUtil.isNotEmpty(userOrderTranVos)) {
            nextId = configEventConfig.getProgressId();
            return nextId;
        }
        //条件 无 购物记录
        if (Constant.INT_TWO.equals(configEventConfig.getCondition()) && PublicUtil.isEmpty(userOrderTranVos)) {
            nextId = configEventConfig.getProgressId();
            return nextId;
        }
        //条件 还没显示购物记录
        if (Constant.INT_THREE.equals(configEventConfig.getCondition()) && PublicUtil.isNotEmpty(userOrderTranVos) && PublicUtil.isNotEmpty(userOrderTranVos.getUserOrderList().stream().filter(d -> d.getUsed() == Constant.INT_ZERO).collect(Collectors.toSet()))) {
            nextId = configEventConfig.getProgressId();
            return nextId;
        }
        //条件 道具高于
        if (Constant.INT_FOUR.equals(configEventConfig.getCondition())) {
            nextId = configEventConfig.getProgressId();
            return nextId;
        }
        //条件 道具低于
        if (Constant.INT_FIVE.equals(configEventConfig.getCondition())) {
            nextId = configEventConfig.getProgressId();
            return nextId;
        }
        return nextId;
    }

}
